import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XmlTest {
	public static void main(String [] args) throws SAXException, IOException, ParserConfigurationException{
		String txt = "<result><trafficmessage><linkid>1616</linkid><datetime>2011-04-13 03:31:06</datetime><status>M</status></trafficmessage></result>";
		System.out.println(parseCLXML(txt));
	}
	
	public static String parseCLXML(String clXML) throws SAXException, IOException, ParserConfigurationException{
    	String cl = "";
//    	if(!linkXML.equals("Information Not Available")){
	    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource();
	        is.setCharacterStream(new StringReader(clXML));
	
	        Document doc = db.parse(is);
	        NodeList nodes = doc.getElementsByTagName("trafficmessage");
	
	        // Get only one linkId
	        for (int i = 0; i < nodes.getLength(); i++) {
	           Element element = (Element) nodes.item(i);
	           String txt = getTextValue(element, "status");
	           if(txt!= null)
	        	   cl += "##"+txt;
	        }
//	    }else{
//	    	linkId = "0";
//	    }
    	
    	return cl;
    }

    private static String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}
}
